import request from 'umi-request';
import {getLocalstorage} from '@/utils/localstorage';
import orderBy from 'lodash/orderBy';
import moment from 'moment';

export async function getPersonSecurityList(params) {
  return request('/api/member/api-member/member/getPersonSecurity', {
    method: 'post',
    data:{
      pageNum: params.current,
      pageSize: params.pageSize,
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getPersonFundsList(params) {
  return request('/api/member/api-member/member/getPersonFunds', {
    method: 'post',
    data:{
      pageNum: params.current,
      pageSize: params.pageSize,
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getPaySlipList(params) {
  return request('/api/member/api-member/member/getEmployeeSalary', {
    method: 'post',
    data:{
      yymm:params.date && params.date[0],
      endyymm:params.date && params.date[1],
      authToken:getLocalstorage("authToken"),
    },
    requestType:'form'
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      const newList = data.list.map((item)=>{
        let time = item.month.replace('年','-').replace('月','')
        return {...item, time:moment(time).format('YYYY-MM')}
      })
      return {
        current: data.currPage,
        data: orderBy(newList,['time'], ['desc']),
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getSalaryChangeList(params) {
  return request(`/api/member/api-member/member/getSalaryChangeList`, {
    method: 'get',
    params:{
      pageNum: params.current,
      pageSize: params.pageSize,
      dateStart:params.data && params.data[0],
      dateEnd:params.data && params.data[1],
      authToken:getLocalstorage("authToken"),
    }
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getEmployeeSalaryModule() {
  return request(`/api/member/api-member/member/getEmployeeSalaryModule`, {
    method: 'get',
    params:{
      authToken:getLocalstorage("authToken"),
    }
  }).then((res) =>{
    return res.data;
  });
}

export async function checkAuthToken(authToken) {
  return request(`/api/member/api-member/member/checkAuthToken`, {
    method: 'post',
    data:{
      authToken:authToken ? authToken : null,
    },
    requestType:'form'
  }).then((res) =>{
    return res;
  });
}

export async function saveAndUpdatePwdOrCheck(pwd,status) {
  let url = status === 'noEntity' ? 'savePwd' : 'checkPwd';
  return request(`/api/member/api-member/member/` + url , {
    method: 'post',
    data: pwd,
    requestType:'form'
  }).then((res) =>{
    return res;
  });
}

export async function queryComputerList(params,type) {
  return request('/api/member/api-member/member/getComputerSubsidyPage', {
    method: 'get',
    params:{
      pageNum: params.current,
      pageSize: params.pageSize,
      type:type
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function saveOrUpdateCredential(params) {
  return request('/api/member/api-member/member/saveAndUpdateComputerSubsidy', {
    method: 'POST',
    data: params,
  }).then((res) =>{
    return res;
  });
}

export async function deleteCredential(id) {
  return request(`/api/member/api-member/member/daleteComputerSubsidy?id=`+id, {
    method: 'delete',
  }).then((res) => {
    return res;
  });
}

export async function memberSubsidyBpm(id) {
  return request('/api/member/api-member/bpm/memberSubsidyBpm', {
    method: 'get',
    params: {id},
    requestType: 'form'
  }).then((res) => {
    return res;
  });
}

export async function getComputerSubsidyEndDate(type) {
  return request('/api/member/api-member/member/getComputerSubsidyEndDate', {
    method: 'get',
    params:{
      type:type
    }
  }).then((res) => {
    return res.data;
  });
}

export async function getCheckCode() {
  return request(`/api/member/api-member/member/getCheckCode` , {
    method: 'post',
  }).then((res) =>{
    return res;
  });
}

export async function updatePwd(data) {
  return request(`/api/member/api-member/member/updatePwd` , {
    method: 'post',
    data:data,
    requestType: 'form'
  }).then((res) =>{
    return res;
  });
}
